<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">  
<html >
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
        <title>Saade: Captura de facturas de clientes</title>
        <link rel="stylesheet" type="text/css" href="css/index.css" />
        <link rel="stylesheet" type="text/css" href="css/form.css" />  
        <link rel="stylesheet" href="css/example.css" TYPE="text/css" MEDIA="screen">
        <script type="text/javascript" src="js/bsn.AutoSuggest_c_2.0.js"></script>
        <script src="js/jquery-1.2.6.min.js" type="text/javascript"></script>
        <script type="text/javascript" src="js/tabber.js"></script>
        <script type="text/javascript">

            /* Optional: Temporarily hide the "tabber" class so it does not "flash"
            on the page as plain HTML. After tabber runs, the class is changed
            to "tabberlive" and it will appear. */

            document.write('<style type="text/css">.tabber{display:none;}<\/style>');
        </script>
        <script>

            function cerrar()
            {
                parent.$.fancybox.close();
            }
        </script>
        <script type="text/javascript" src="js/jquery.tools.min.js"></script>
    </head>
    <body style="margin: 0px; padding: 0px;"  >
        <?php
            include_once('db.php');
            
            $fecha_factura = $_POST['fecha'];
            $id_empresa = $_POST['empresa_b'];
            
            
            if( intval(date("d",strtotime($fecha_factura))) < 16){
                $corte_q =  'Q1/'. date("m",strtotime($fecha_factura)) .'/'. date("y",strtotime($fecha_factura)); 
            }else{
                $corte_q =  'Q2/'. date("m",strtotime($fecha_factura)) .'/'. date("y",strtotime($fecha_factura)); 
            }

            $result2 = mysql_query("SELECT * FROM corte where fecha_corte < '$fecha_factura' ORDER BY id_corte DESC LIMIT 1");
            while ($row2 = mysql_fetch_array($result2)) {
                $corte_actual = $row2['id_corte']; 
            }

            $saldo_anterior = 0; 

            if ($corte_actual > 1){
                $result2 = mysql_query("SELECT * FROM quincena_calculo where id_empresa = ".$id_empresa." ORDER BY id_quincena_calculo DESC LIMIT 1");
                while ($row2 = mysql_fetch_array($result2)) {
                    $saldo_anterior = $row2['cantidad_restante'] + $row2['intereses_generados']; 
                }
            }

            $sql = "SELECT * from empresa where id_empresa = " .$id_empresa;
            $result = mysql_query($sql);                                                            
            while ($row = mysql_fetch_array($result)) {
                $nombre_empresa = $row['nombre'];
            }

        ?>
        <table width="950">
            <tr>
                <td align="right"><?php echo date("d/m/Y") ?></td>
            </tr>
        </table>
        <center><div class="header_reporte">
                <p style="font-size: 24px; font-weight: bold"><?php echo $nombre_empresa; ?></p>
                <p style="font-size: 18px; font-weight: bold">Reporte de servicios realizados para el corte <?php echo $corte_q; ?> </p>
            </div></center>
        <br><br><br>
        <table align="center" >
            <tr >
                <td style="text-decoration: underline;" width="60" align="left">Factura</td>
                <td style="text-decoration: underline;" width="60" align="left">Servicio </td>
                <td style="text-decoration: underline;" width="250" align="left">Nombre</td>
                <td style="text-decoration: underline;" width="50" align="right">Pago</td>
                <td style="text-decoration: underline;" width="90" align="right">Fecha</td>
                <td style="text-decoration: underline;" width="90" align="right">Monto</td>
                <td style="text-decoration: underline;" width="100" align="right">SdoRestante</td>
                <td style="text-decoration: underline;" width="100" align="right">Ab.Quincenal</td>
                <td style="text-decoration: underline;" width="100" align="right">SdoEstimado</td>
            </tr>


            <?php
                $total_monto = 0;
                $total_saldo_restante = 0;
                $total_abono_quincenal = 0;
                $total_saldo_estimado =0;

                /*
                $sql = "SELECT e.id_factura_empresa, a.id_factura_cliente, CONCAT(c.nombre, ' ',c.ap_paterno, ' ', c.ap_materno) as nombre, CONCAT(a.numero_quincena,'/',d.quincenas_pago) as pago, b.fecha, ROUND(b.total,2) as monto, ROUND(b.total - ((a.numero_quincena -1) * a.total),2) as saldo_restante, ROUND(a.total,2) as abono_quincenal, ROUND(b.total - (a.numero_quincena  * a.total),2) as saldo_estimado 
                FROM quincenas a, factura_cliente b, clientes c, empresa d, factura_empresa e
                WHERE a.id_factura_cliente = b.id_factura_cliente
                AND a.no_empleado = c.no_empleado
                AND a.id_empresa = d.id_empresa
                AND a.fecha = e.fecha
                AND a.id_empresa = e.id_empresa
                AND a.fecha = '". $fecha_factura."'
                AND a.id_empresa =" .$id_empresa;
                 */
               
               $sql ="SELECT a.id_factura_empresa,
       a.id_factura_cliente,
       CONCAT(c.nombre,
              ' ',
              c.ap_paterno,
              ' ',
              c.ap_materno)
          AS nombre,
       CONCAT(a.numero_quincena, '/', d.quincenas_pago) AS pago,
       b.fecha,
       ROUND(b.total, 2) AS monto,
       ROUND(b.total - ((a.numero_quincena - 1) * a.total), 2)
          AS saldo_restante,
       ROUND(a.total, 2) AS abono_quincenal,
       ROUND(b.total - (a.numero_quincena * a.total), 2) AS saldo_estimado
  FROM quincenas a
      LEFT JOIN factura_cliente b
          ON a.id_factura_cliente = b.id_factura_cliente
      LEFT JOIN clientes c
          ON a.no_empleado = c.no_empleado
      LEFT JOIN empresa d
          ON a.id_empresa = d.id_empresa
 WHERE a.fecha = '". $fecha_factura."'
                AND a.id_empresa =" .$id_empresa;
                
                $result3 = mysql_query($sql);
                while ($row3 = mysql_fetch_array($result3)) {
                    echo '<tr>';
                    echo '<td style="font-size: 12px;" align="left">' . $row3['id_factura_empresa'] . '</td>';  
                    echo '<td style="font-size: 12px;" align="left">' . $row3['id_factura_cliente'] . '</td>';
                    echo '<td style="font-size: 12px;" align="left">' . $row3['nombre'] . '</td>';
                    echo '<td style="font-size: 12px;" align="right">' . $row3['pago'] . '</td>';
                    echo '<td style="font-size: 12px;" align="right">' . date("d/m/Y",strtotime($row3['fecha']))  . '</td>';
                    echo '<td style="font-size: 12px;" align="right">$' . number_format($row3['monto'],2) . '</td>';  
                    echo '<td style="font-size: 12px;" align="right">$' . number_format($row3['saldo_restante'],2) . '</td>';  
                    echo '<td style="font-size: 12px;" align="right">$' . number_format($row3['abono_quincenal'],2) . '</td>';
                    echo '<td style="font-size: 12px;" align="right">$' . number_format($row3['saldo_estimado'],2) . '</td>';  
                    echo '</tr>'; 

                    $total_monto = $total_monto + $row3['monto'];
                    $total_saldo_restante = $total_saldo_restante + $row3['saldo_restante'];
                    $total_abono_quincenal = $total_abono_quincenal + $row3['abono_quincenal'];
                    $total_saldo_estimado = $total_saldo_estimado + $row3['saldo_estimado'];
                }

                $total_a_pagar = $total_abono_quincenal + $saldo_anterior;
            ?>
        </table>
        <hr width="1000">
        <table align="center" >
            <tr >
                <td style="text-decoration: underline;" width="60" align="left"></td>
                <td style="text-decoration: underline;" width="60" align="left"></td>
                <td style="text-decoration: underline;" width="250" align="left"></td>
                <td style="text-decoration: underline;" width="50" align="right"></td>
                <td style="text-decoration: underline;" width="90" align="right"></td>
                <td  width="90" align="right" style="font-size: 12px; font-weight: bold; "><?php echo '$'.number_format($total_monto,2); ?></td>
                <td  width="100" align="right" style="font-size: 12px; font-weight: bold;"><?php echo '$'. number_format($total_saldo_restante,2); ?></td>
                <td  width="100" align="right" style="font-size: 12px; font-weight: bold;"><?php echo '$'.number_format($total_abono_quincenal,2); ?></td>
                <td  width="100" align="right" style="font-size: 12px; font-weight: bold;"><?php echo '$'.number_format($total_saldo_estimado,2); ?></td>
            </tr>
        </table>          
        <br><br> <br>
        <table align="center">
            <tr>
                <td width="130" style="font-size: 14px; font-weight: bold;">Monto Total:</td>
                <td width="130" style="font-size: 14px; font-weight: bold;"><?php echo '$'.number_format($total_monto,2); ?></td>
                <td width="130" style="font-size: 14px; font-weight: bold;">Abono Quincenal:</td>
                <td width="130" style="font-size: 14px; font-weight: bold;"><?php echo '$'.number_format($total_abono_quincenal,2); ?></td>
            </tr>
            <tr>
                <td width="130" style="font-size: 14px; font-weight: bold;">Saldo Restante:</td>
                <td width="130" style="font-size: 14px; font-weight: bold;"><?php echo '$'. number_format($total_saldo_restante,2); ?></td>
                <td width="130" style="font-size: 14px; font-weight: bold;">Cargo de Retraso:</td>
                <td width="130" style="font-size: 14px; font-weight: bold;"><?php echo '$'.number_format($saldo_anterior,2); ?></td>
            </tr>
            <tr>
                <td width="130" style="font-size: 14px; font-weight: bold;">Saldo Estimado:</td>
                <td width="130" style="font-size: 14px; font-weight: bold;"><?php echo '$'.number_format($total_saldo_estimado,2); ?></td>
                <td width="130" style="font-size: 14px; font-weight: bold;"></td>
                <td width="130" style="font-size: 14px; font-weight: bold;"></td>
            </tr>
        </table>
        <br><br><br>
        <table width="700" align="center">
            <tr>
                <td width="130"></td>
                <td width="130" align="right" style="font-size: 20px; font-weight: bold; padding: 10px; border: solid 1px black;"><?php echo 'TOTAL A PAGAR: $'.number_format($total_a_pagar,2); ?></td>
            </tr>
        </table>
    </body>
</html>
